package com.ctbu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ctbu.api.UserInfoService;
import com.ctbu.entity.FinancialProduct;
import com.ctbu.entity.FinancialTrades;
import com.ctbu.entity.Users;
import com.ctbu.mapper.FinancialProductMapper;
import com.ctbu.mapper.FinancialTradesMapper;
import com.ctbu.service.FinancialTradesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctbu.tools.ResponseResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 金融衍生品交易表，包含交易ID、产品ID、用户ID、约定价格和交易日期等信息 服务实现类
 * </p>
 *
 * @author lli
 * @since 2024年06月27日
 */
@Service
public class FinancialTradesServiceImpl extends ServiceImpl<FinancialTradesMapper, FinancialTrades> implements FinancialTradesService {

    @Autowired
    private FinancialTradesMapper financialTradesMapper;
    @Autowired
    private FinancialProductMapper financialProductMapper;

    @Autowired
    private UserInfoService userInfoService;




    @Override
    public ResponseResult<Object> selectList(int pageNum, int pageSize, String tradeName, String tradeDate) {
        //开启分页
        PageHelper.startPage(pageNum,pageSize);
        //条件查询
        LambdaQueryWrapper<FinancialTrades> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        if(!tradeName.equals("")){
            lambdaQueryWrapper.like(FinancialTrades::getTradeName,tradeName);
        }

        if(!tradeDate.equals("")){
            lambdaQueryWrapper.eq(FinancialTrades::getTradeDate,tradeDate);
        }

        List<FinancialTrades> list = financialTradesMapper.selectList(lambdaQueryWrapper);

        list.forEach(financialTrades -> {
            //处理类型的数据
            FinancialProduct financialProduct=financialProductMapper.selectById(financialTrades.getProductId());
            //产品信息封装到交易信息中
            financialTrades.setFinancialProduct(financialProduct);

            //用户信息封装到交易信息中
            Users users= userInfoService.byId(financialTrades.getUserId());
            financialTrades.setUsers(users);

        });


        //将查询结果封装到对应分页中
        PageInfo<FinancialTrades> pageInfo = new PageInfo<>(list);
        return ResponseResult.success(pageInfo,"查询成功");


    }


}
